package com.keepers.repair.service;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.keepers.repair.activity.MainNewActivity;
import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.model.UserModel;
import com.keepers.repair.net.LogUtil;

import java.util.List;

/**
 * auth aboom
 * date 2018/12/11
 */
public class NotifyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        LogUtil.e("收到广播通知NotifyReceiver.onReceive()-S-Action:" + action);
        if (!checkCoreServiceExist(context)) {
            CoreService.startCoreService(context);
        }
        if (!checkMainActivityExist(context)) {
            UserModel loginUserInfo = SpHelper.getLoginUserInfo();
            if (!TextUtils.isEmpty(loginUserInfo.Gid)) {
                LogUtil.e("NotifyReceiver start main");
                Intent intentMainActivity = new Intent(context, MainNewActivity.class);
                intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentMainActivity);
            }
        }
        if (!checkKeeperServiceExist(context)) {

        }
        SocketService.startSocketService(context);
//        TCPClient tcpClient = SocketService.tcpClient;
//        if (tcpClient != null && !tcpClient.isConnected()) {
//            LogUtil.e("NotifyReceiver tcpClient init");
//            SocketService.initSocket();
//        }
    }


    public boolean checkMainActivityExist(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(100);
        int num = taskInfoList.size();
        String className;
        for (int i = 0; i < num; i++) {
            ActivityManager.RunningTaskInfo taskIno = taskInfoList.get(i);
            className = taskIno.baseActivity.getClassName();
            if (className.equals(MainNewActivity.class.getName())) {
                LogUtil.e("NotifyReceiver checkMainActivityExist(true)-E-");
                return true;
            }
        }
        return false;
    }

    public boolean checkCoreServiceExist(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> taskInfoList = am.getRunningServices(200);
        int num = taskInfoList.size();
        String className = "";
        for (int i = 0; i < num; i++) {
            ActivityManager.RunningServiceInfo taskIno = taskInfoList.get(i);
            className = taskIno.service.getClassName();
            if (className.equals(CoreService.class.getName())) {
                LogUtil.e("NotifyReceiver checkCoreServiceExist(true)-E-");
                return true;
            }
        }
        return false;
    }

    public boolean checkKeeperServiceExist(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> taskInfoList = am.getRunningServices(200);
        int num = taskInfoList.size();
        String className = "";
        for (int i = 0; i < num; i++) {
            ActivityManager.RunningServiceInfo taskIno = taskInfoList.get(i);
            className = taskIno.service.getClassName();
            if (className.equals(SocketService.class.getName())) {
                LogUtil.e("NotifyReceiver checkKeeperServiceExist(true)-E-");
                return true;
            }
        }
        return false;
    }
}

